package com.wjy.qs.controller;

import com.wjy.common.base.controller.BaseController;
import com.wjy.common.result.ResultBuilder;
import com.wjy.common.service.RestTemplateService;
import com.wjy.qs.entity.Author;
import com.wjy.qs.service.IAuthorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ybxxszl
 * @since 2019-02-21
 */
@Api(tags = "作者控制")
@RestController
@RequestMapping("/qs/author")
public class AuthorController extends BaseController {

    @Autowired
    @Qualifier(value = "authorServiceImpl")
    private IAuthorService authorService;

    @Autowired
    @Qualifier(value = "restTemplateServiceImpl")
    private RestTemplateService restTemplateService;

    // @Autowired
    // private RestTemplateUtil restTemplateUtil;

    // @Autowired
    // private PropertiesUtil propertiesUtil;

    @ApiOperation(value = "添加作者")
    @RequestMapping(value = "/insertAuthor", method = RequestMethod.POST)
    public ResultBuilder insertAuthor(@RequestBody Author author) throws Exception {
        // ResultBuilder resultBuilder = restTemplateUtil.postWithHeader(propertiesUtil.getProperty("server.url",
        // String.class, null) + "/qs/author/insertAuthor", author, ResultBuilder.class);
        // try {
        // authorService.insertAuthor(author);
        // return ResultBuilder.success("添加成功");
        // } catch (Exception e) {
        // log.error(e);
        // e.printStackTrace();
        // return ResultBuilder.error(e);
        // }
        authorService.insertAuthor(author);
        return ResultBuilder.success("添加成功");
    }

    @ApiOperation(value = "更新作者")
    @RequestMapping(value = "/updateAuthor", method = RequestMethod.POST)
    public ResultBuilder updateAuthor(@RequestBody Author author) throws Exception {
        // try {
        // authorService.updateAuthor(author);
        // return ResultBuilder.success("更新成功");
        // } catch (Exception e) {
        // log.error(e);
        // e.printStackTrace();
        // return ResultBuilder.error(e);
        // }
        authorService.updateAuthor(author);
        return ResultBuilder.success("更新成功");
    }

    @ApiOperation(value = "删除作者")
    @RequestMapping(value = "/deleteAuthor", method = RequestMethod.GET)
    public ResultBuilder deleteAuthor(@RequestParam(value = "authorId") String authorId) throws Exception {
        // try {
        // authorService.deleteAuthor(authorId);
        // return ResultBuilder.success("删除成功");
        // } catch (Exception e) {
        // log.error(e);
        // e.printStackTrace();
        // return ResultBuilder.error(e);
        // }
        authorService.deleteAuthor(authorId);
        return ResultBuilder.success("删除成功");
    }

    @ApiOperation(value = "选择作者")
    @RequestMapping(value = "/selectAuthor", method = RequestMethod.GET)
    public ResultBuilder selectAuthor(@RequestParam(value = "authorId") String authorId) throws Exception {
        // ResultBuilder resultBuilder = restTemplateUtil.getWithHeader(propertiesUtil.getProperty("server.url",
        // String.class, null) + "qs/author/selectAuthor?authorId=" + authorId, ResultBuilder.class);
        // try {
        // Author author = restTemplateService.getSelectAuthor(authorId);
        // Author author = authorService.selectAuthor(authorId);
        // return ResultBuilder.success(author);
        // } catch (Exception e) {
        // log.error(e);
        // e.printStackTrace();
        // return ResultBuilder.error(e);
        // }
        Author author = authorService.selectAuthor(authorId);
        return ResultBuilder.success(author);
    }

    @ApiOperation(value = "选择作者列表")
    @RequestMapping(value = "/selectAuthorList", method = RequestMethod.GET)
    public ResultBuilder selectAuthorList() throws Exception {
        // try {
        // List<Author> authorList = authorService.selectAuthorList();
        // return ResultBuilder.success(authorList);
        // } catch (Exception e) {
        // log.error(e);
        // e.printStackTrace();
        // return ResultBuilder.error(e);
        // }
        List<Author> authorList = authorService.selectAuthorList();
        return ResultBuilder.success(authorList);
    }

}
